<?php
// +----------------------------------------------------------------------
// | my [负责builder的处理]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: $this <498944516.qq.com>
//  
// +----------------------------------------------------------------------


/**
 * 处理搜索相关
 * @return 
 * @author  $this <498944516@qq.com>
 * 《无题》
 *     ——致查查
 * 秋日红叶暖，
 * 黎黄楼顶，沐浴斜阳，望江滩。
 * 天道大秦，岂畏难？
 * 三年弹指，然一挥之间。
 *
 * 冬雪梧桐染，
 * 常青五院，共谱春秋，写长港。
 * 夫妻同心，斩荆棘！
 * 蓦然回首，当一马平川。
 * 
 */
function myMap() {
    $param = input('param.'); // 获取变量
    if ( isset($param['file']) ) unset($param['file']);
   
    // 处理分页
    $limit = $param['pageSize'];
    $offset = ($param['pageNumber']-1) * $limit;
   
    $map = [];
    if ( isset($param['filter']) ) {
        $filter = json_decode($param['filter'], true);

        $like = $in = array();
        if ( isset($filter['like[]']) ) {
            $like = explode(",", $filter['like[]']);
            unset($filter['like[]']);
        }
        if ( isset($filter['in[]']) ) {
            $in = explode(",", $filter['in[]']);
            unset($filter['in[]']);
        }

        if ( isset($filter["time[]"]) ) {
            $fields = explode(",", $filter["time[]"]);

            foreach ( $fields as $field ) {
                if ( isset($filter[$field."_start"]) ) {
                    $time_start = strtotime($filter[$field.'_start']);
                    $map[$field][] = ['egt', $time_start];
                    unset($filter[$field.'_start']);
                }
                if ( isset($filter[$field."_end"]) ) {
                    $time_end = strtotime($filter[$field.'_end']);
                    $map[$field][] = ['elt', $time_end];
                    unset($filter[$field.'_end']);
                }
            }

            unset($filter["time[]"]);
        }
        
        foreach ( $filter as $key => $value ) {
            $key = str_replace(['[', ']'], ['', ''], $key);

            if ( in_array($key, $like) ) {
                $map[$key] = ['like', '%'.$value.'%'];
            } else if ( in_array($key, $in) ) {
                $map[$key] = ['in', $value];
            } else {
                $map[$key] = $value;
            }
        }
    }

    return [
        'map'=>$map,
        'limit'=>$limit,
        'offset'=>$offset,
    ];
}

/**
 * 根据搜索条件获取结果数据
 * @return boolean true-管理员，false-非管理员
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function myData($my,$model,$where=[],$field=true,$order='id desc',$debug=0)
{
    $map=myMap();

    if (!is_admin(session('id'))) $where['aid']=session('aid');
    $where['status']=['EGT',0];
    
    $map['map']=array_merge ($where,$map['map']);
   
    $table=db($model);
    $return['rows'] =$table->field($field)->where($map['map'])->limit($map['offset'],$map['limit'])->order($order)->select();
    if($debug==1) mymysql($table);
    $return['total'] = $table->where($map['map'])->limit($map['offset'],$map['limit'])->count();
    $return['rows'] = $my->data($return['rows']);
    return $return;
}


function mySelect($db,$key='id',$value='title'){
        $map['aid']=session('aid');
        $map['status']=['EGT',0];
        $list=db($db)->field($key.','.$value)->where($map)->select();
        $list =array_column($list, $value, $key);
        return $list;
    }

function myEdit($model,$id,$default=[])
{
    if ( $id ) {
        $data = model($model)->find($id);
    } else {
        $data = $default;
    }
    return $data;
}









